﻿/**
 * 为语音转文本组件设置appKey和token
 */

var speech = null;
const speech_setting = "speech_setting";

async function proceColumns(columns){
    columns.filter(x=>x.control=="textarea" && x?.textarea?.audio2Text==true && (!x?.textarea?.token)).forEach(async c => {
        if(!speech){
            speech = sessionStorage.getItem(speech_setting)
            if(speech){
                const ss = speech.split("|");
                speech = {
                    SpeechAppKey:ss[0],
                    SpeechToken:ss[1]
                }
            }else{
                //speech = await $server.call("/Common/GetSysSetting",{key:"SpeechAppKey;SpeechToken"})
                speech = await $server.call("/Speech/getConfig",{key:"SpeechAppKey;SpeechToken"})
                sessionStorage.setItem(speech_setting, speech.SpeechAppKey+"|"+speech.SpeechToken)
            }
        }
        let attr = c[c.control] || {};
        attr.appKey = attr.appKey || speech.SpeechAppKey;
        attr.token = attr.token || speech.SpeechToken;
        c[c.control] = attr;
    });
}

async function process(tb, tbname) {
    if (!tb) return;
    if(tb.columns){
        await proceColumns(tb.columns);
    }

    Object.keys(tb).forEach(async key=>{
        if(key.startsWith("form")){
            const columns = tb[key]?.columns;
            if(columns){
                await proceColumns(columns);
            }
        }
    })
}


export { process };